﻿namespace OleDbDAL.Sales
{
    using DBUtility;
    using IDal.Sales;
    using Model.Sales;
    using System;
    using System.Data;
    using System.Text;

    public class TestDunkCommApp : ITestDunkComm
    {
        private string dbname = "Systems";

        public void Add(TestDunkCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_TestDunkComm(");
            builder.Append("TestDunkId,LotNum,SupplierId,DlId,CommId,CommName,Amount,AmoutUnit,Cost,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.TestDunkId + ",");
            builder.Append("'" + model.LotNum + "',");
            builder.Append("'" + model.SupplierId + "',");
            builder.Append("" + model.DlId + ",");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.CommName + "',");
            builder.Append("" + model.Amount + ",");
            builder.Append("" + model.AmoutUnit + ",");
            builder.Append("" + model.Cost + ",");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int TestDunkCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_TestDunkComm ");
            builder.Append(" where TestDunkCommId=" + TestDunkCommId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TestDunkComm ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by TestDunkCommId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "TestDunkCommId", "T_TestDunkComm", swhere);
        }

        public TestDunkCommEnt GetModel(int TestDunkCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select TestDunkCommId,TestDunkId,LotNum,SupplierId,DlId,CommId,CommName,Amount,AmoutUnit,Cost,Remark from T_TestDunkComm ");
            builder.Append(" where TestDunkCommId=" + TestDunkCommId);
            TestDunkCommEnt ent = new TestDunkCommEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.TestDunkCommId = TestDunkCommId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["TestDunkId"].ToString() != "")
                {
                    ent.TestDunkId = int.Parse(table.Rows[0]["TestDunkId"].ToString());
                }
                ent.LotNum = table.Rows[0]["LotNum"].ToString();
                ent.SupplierId = table.Rows[0]["SupplierId"].ToString();
                if (table.Rows[0]["DlId"].ToString() != "")
                {
                    ent.DlId = int.Parse(table.Rows[0]["DlId"].ToString());
                }
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.CommName = table.Rows[0]["CommName"].ToString();
                if (table.Rows[0]["Amount"].ToString() != "")
                {
                    ent.Amount = decimal.Parse(table.Rows[0]["Amount"].ToString());
                }
                if (table.Rows[0]["AmoutUnit"].ToString() != "")
                {
                    ent.AmoutUnit = int.Parse(table.Rows[0]["AmoutUnit"].ToString());
                }
                if (table.Rows[0]["Cost"].ToString() != "")
                {
                    ent.Cost = decimal.Parse(table.Rows[0]["Cost"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_TestDunkComm ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "TestDunkCommId");
        }

        public void Update(TestDunkCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_TestDunkComm set ");
            builder.Append("TestDunkId=" + model.TestDunkId + ",");
            builder.Append("LotNum='" + model.LotNum + "',");
            builder.Append("SupplierId='" + model.SupplierId + "',");
            builder.Append("DlId=" + model.DlId + ",");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("Amount=" + model.Amount + ",");
            builder.Append("AmoutUnit=" + model.AmoutUnit + ",");
            builder.Append("Cost=" + model.Cost + ",");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where TestDunkCommId=" + model.TestDunkCommId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

